package cn.hrlly.tao2shou.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.adapter.MyCommoditiesAdapter;
import cn.hrlly.tao2shou.domain.Commodities;
import cn.hrlly.tao2shou.domain.CommodityList;
import cn.hrlly.tao2shou.other.Config;
import cn.hrlly.tao2shou.presenter.PublishCommoditiesListPresenter;
import cn.hrlly.tao2shou.presenter.PublishCommoditiesListPresenterImpl;
import cn.hrlly.tao2shou.utils.NetUtil;
import cn.hrlly.tao2shou.view.base.BaseActivity;
import cn.hrlly.tao2shou.view.view_interface.PublishCommoditiesListView;

public class MyCommodityActivity extends BaseActivity implements PublishCommoditiesListView{

    @Bind(R.id.lvCommodity)
    ListView lvCommodity;
    private PublishCommoditiesListPresenter publishCommoditiesListPresenter;
    private ArrayList<Commodities> list;
    private MyCommoditiesAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commodity);
        //绑定控件
        ButterKnife.bind(this);
        //初始化
        publishCommoditiesListPresenter=new PublishCommoditiesListPresenterImpl(this,this);
        list=new ArrayList<Commodities>();
        clickListview();
        if(NetUtil.ping(this)){
            publishCommoditiesListPresenter.showPublishCommodities();
        }else {
            publishCommoditiesListPresenter.showCachePublishCommodities();
        }
    }

    //listview点击事件
    public void clickListview(){
        lvCommodity.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(MyCommodityActivity.this,MyCommodityDetailsActivity.class);
                intent.putExtra(Config.key_release_id,list.get(position).getReleaseId());
                startActivity(intent);
            }
        });
    }

    //以下是对activity的操作
    @Override
    public void showListView(CommodityList commodityList) {
        list.addAll(commodityList.getCommodities());
        adapter=new MyCommoditiesAdapter(list,this);
        lvCommodity.setAdapter(adapter);
    }

    @Override
    public void showMessage(String message) {
        Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showMessage(int message) {
        Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
    }
}
